package com.jy.api.service.impl;

import com.jy.api.dao.NetSchedulingTransactionDao;
import com.jy.api.dao.StoreDao;
import com.jy.api.domain.NetSchedulingTransaction;
import com.jy.api.domain.Store;
import com.jy.api.service.NetSchedulingTransactionService;
import com.jy.api.util.JodaUtils;
import com.jy.api.wechat.service.Gzh1WxService;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @author xiaohua
 * @data 2025/3/10 0010 18:16
 */
@Slf4j
@Service
public class NetSchedulingTransactionServiceImpl implements NetSchedulingTransactionService {
    @Autowired
    private Gzh1WxService wxService;
    @Autowired
    private NetSchedulingTransactionDao netSchedulingTransactionDao;
    @Autowired
    private StoreDao storeDao;

    @Async
    @Override
    public void sendNetSchedulingTransaction(NetSchedulingTransaction transaction) {
        Store store = storeDao.findOne(transaction.getStoreId());
        if (null == transaction.getOpenId()) {
            transaction.setOpenId(new ArrayList<>());
        }
        if (transaction.getType() == 1 || transaction.getType() == 2) {
            for (String s : transaction.getOpenId()) {
                WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                        .templateId("g1RDEFb7fYvaqLwk4p81ROg6Wc5e2cw347cCu0prKEo")
                        .toUser(s)
                       // .url(String.format("https://admin.huoxingy.com/static/getSite/index.html?id=%s", transaction.getId()))
                        .data(dealData(store.getName(), transaction, 0))
                        .build();
                try {
                    wxService.getTemplateMsgService().sendTemplateMsg(templateMessage);
                    Thread.sleep(100);
                } catch (Exception ex) {
                    log.error("排号模板发放异常{}", ex);
                }
            }
        }
        List<NetSchedulingTransaction> findAll = netSchedulingTransactionDao.findAllByStoreIdAndAreaIdAndTypeAndAddTimeBetweenOrderByAddTimeAsc(transaction.getStoreId(), transaction.getAreaId(), 0, JodaUtils.getStartOfYesterdayDate(),JodaUtils.getEndOfTodayDateTime());
        for (NetSchedulingTransaction netSchedulingTransaction : findAll) {
            for (String s : netSchedulingTransaction.getOpenId()) {
                WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                        .templateId("g1RDEFb7fYvaqLwk4p81ROg6Wc5e2cw347cCu0prKEo")
                        .toUser(s)
                      //  .url(String.format("https://admin.huoxingy.com/static/getSite/index.html?id=%s", transaction.getId()))
                        .data(dealData(store.getName(), netSchedulingTransaction,  findAll.indexOf(netSchedulingTransaction)))
                        .build();
                try {
                    wxService.getTemplateMsgService().sendTemplateMsg(templateMessage);
                    Thread.sleep(100);
                } catch (Exception ex) {
                    log.error("排号模板发放异常{}", ex);
                }
            }
        }
    }


    private List<WxMpTemplateData> dealData(String storeName, NetSchedulingTransaction transaction, long count) {
        List<WxMpTemplateData> wxMpTemplateDataList = new ArrayList<>();
        WxMpTemplateData wxMpTemplateData2 = new WxMpTemplateData("thing2", storeName);
        WxMpTemplateData wxMpTemplateData3 = new WxMpTemplateData("character_string11", transaction.getNum());
        WxMpTemplateData wxMpTemplateData4 = new WxMpTemplateData("thing7", transaction.getArea());
        WxMpTemplateData wxMpTemplateData5 = new WxMpTemplateData("character_string5", count + "");
        String type = "";
        switch (transaction.getType()) {
            case 0:
                type = "排队中";
                break;
            case 1:
                type = "叫号中";
                break;
            case 2:
                type = "已过号";
                break;
        }
        WxMpTemplateData wxMpTemplateData6 = new WxMpTemplateData("const6", type);
        wxMpTemplateDataList.add(wxMpTemplateData2);
        wxMpTemplateDataList.add(wxMpTemplateData3);
        wxMpTemplateDataList.add(wxMpTemplateData4);
        wxMpTemplateDataList.add(wxMpTemplateData5);
        wxMpTemplateDataList.add(wxMpTemplateData6);
        return wxMpTemplateDataList;
    }
}
